﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Navigation;
using System.Windows.Shapes;
using System.Collections.ObjectModel;
using Microsoft.Win32;
using ExEdit.Engine;

namespace ExEdit.UI
{
    /// <summary>
    /// Interaction logic for MainWindow.xaml
    /// </summary>
    public partial class MainWindow : Window
    {
        public MainWindow()
        {
            InitializeComponent();
        }
        string wybranyobrazek = string.Empty;
        private void button1_Click(object sender, RoutedEventArgs e)
        {

        }

        private void mniPickImage_Click(object sender, RoutedEventArgs e)
        {
            ShowPickUpImageDialog();
        }

        private void ShowPickUpImageDialog()
        {
            try
            {
                OpenFileDialog dlg = new OpenFileDialog();
                dlg.Filter = "jpg elements (*.jpg)|*.jpg|All Files (*.*)|*.*";
                if (dlg.ShowDialog().GetValueOrDefault())
                {
                    ItemSearchEngine searcher = new ItemSearchEngine();//
                    var dlgres = dlg;
                    lstImageItems.ItemsSource = searcher.GetSelectedItem(dlg.FileName);
                    //lstImageItems.
                }
            }
            catch (Exception ex)
            {
                MessageBox.Show(ex.Message);
            }
        }

        private void mniClose_Click(object sender, RoutedEventArgs e)
        {
            if (MessageBox.Show("Czy chcesz zamknąć program?",

  "Potwierdzenie", MessageBoxButton.YesNo) == MessageBoxResult.Yes)
            {
                this.Close();
            }

        }

        private void mniSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            ZapiszZmiany();
        }

        private void ZapiszZmiany()
        {

        }


        private void lstImageItems_SelectionChanged(object sender, SelectionChangedEventArgs e)
        {
            if (!string.IsNullOrEmpty(wybranyobrazek) && lstImageItems.SelectedItem == null) return;
            wybranyobrazek = (lstImageItems.SelectedItem as ImageWrapper).ImagePath;
            LoadImageToGrid(wybranyobrazek);
        }


        private void mniAbout_Click(object sender, RoutedEventArgs e)
        {
            About abt = new About();
        }


        /// <summary>
        /// ładuj dane do grida
        /// </summary>
        /// <param name="selectedImagePath"></param>
        private void LoadImageToGrid(string selectedImagePath)
        {
            ExEdit.Engine.ExEditProcessing exEng = new ExEditProcessing();
            dgExifData.ItemsSource = exEng.ReadMetadateFromImage(selectedImagePath);
            ItemSearchEngine searcher = new ItemSearchEngine();//
            var obrazek = searcher.GetSelectedItem(selectedImagePath);
            lstImageItems.ItemsSource = obrazek;
            // lstImageItems.SelectedItem = obrazek[0];
        }

        private void Image_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)
        {
            SaveChanges();
        }


        /// <summary>
        /// zapisz zmiany
        /// </summary>
        private void SaveChanges()
        {
            var exData = dgExifData.ItemsSource as List<ExifMetadataItem>;
            ExEdit.Engine.ExEditProcessing exEng = new ExEditProcessing();

            if (exData != null && !string.IsNullOrEmpty(wybranyobrazek))
            {
                // string sciezkaedytowanegoobrazka = (lstImageItems.SelectedItem as ImageWrapper).ImagePath;
                CleanList();
                string result = exEng.SaveMetadataForImage(exData, wybranyobrazek);
                if (!string.IsNullOrEmpty(result))
                {
                    LoadImageToGrid(result);
                    wybranyobrazek = result;
                    MessageBox.Show("Dane zostały zaktualizowane");
                }
                else
                {
                    MessageBox.Show("Błąd przy zapisie");
                }
            }
        }
        /// <summary>
        /// czysc liste ze zdjeciami
        /// </summary>
        private void CleanList()
        {
            lstImageItems.DataContext = null;
            lstImageItems.ItemsSource = null;
        }

        /// <summary>
        /// przycis wywołujący funkcję zapisującą grafikę
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void btnSaveChanges_Click(object sender, RoutedEventArgs e)
        {
            SaveChanges();
        }


    }
}
